package com.example.assistant;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Iterator;

public class JsonSchemaValidator {

    private static final ObjectMapper mapper = new ObjectMapper();

    /**
     * 校验 JSON 字符串是否符合标准物模型定义
     *
     * @param jsonStr 待校验的 JSON 字符串
     * @return 合法则返回原始字符串，否则返回 "{}"
     */
    public static String validate(String jsonStr) {
        try {
            JsonNode jsonNode = mapper.readTree(jsonStr);

            // 必须包含 device_type 和 property 字段
            if (!jsonNode.has("device_type") || !jsonNode.has("property")) {
                System.out.println("缺少必要字段");
                return "{}";
            }

            String deviceType = jsonNode.get("device_type").asText();
            String property = jsonNode.get("property").asText();

            // 获取物模型定义
            JsonNode schemaNode = DeviceModelLoader.getSchema(deviceType);
            if (schemaNode == null) {
                System.out.println("不支持的设备类型：" + deviceType);
                return "{}";
            }

            JsonNode properties = schemaNode.get("properties");
            if (properties == null || !properties.has(property)) {
                System.out.println("属性不存在于物模型中：" + property);
                return "{}";
            }

            JsonNode propDef = properties.get(property);
            JsonNode valueNode = jsonNode.get("value");

            // 校验值类型与范围
            if (propDef.has("type")) {
                String type = propDef.get("type").asText();

                switch (type) {
                    case "switch":
                        if (!valueNode.isTextual() || (!"on".equals(valueNode.asText()) && !"off".equals(valueNode.asText()))) {
                            System.out.println("开关值必须是 'on' 或 'off'");
                            return "{}";
                        }
                        break;
                    case "int":
                        if (!valueNode.isInt()) {
                            System.out.println("期望整数类型");
                            return "{}";
                        }

                        int intValue = valueNode.asInt();
                        if (propDef.has("min") && intValue < propDef.get("min").asInt()) {
                            System.out.println("数值低于最小值");
                            return "{}";
                        }
                        if (propDef.has("max") && intValue > propDef.get("max").asInt()) {
                            System.out.println("数值高于最大值");
                            return "{}";
                        }
                        break;
                    case "enum":
                        boolean match = false;
                        JsonNode values = propDef.get("values");
                        for (Iterator<JsonNode> it = values.elements(); it.hasNext(); ) {
                            if (it.next().asText().equals(valueNode.asText())) {
                                match = true;
                                break;
                            }
                        }
                        if (!match) {
                            System.out.println("枚举值不在允许范围内");
                            return "{}";
                        }
                        break;
                    default:
                        System.out.println("未知数据类型：" + type);
                        return "{}";
                }
            }

            // 所有校验通过
            return jsonStr;

        } catch (Exception e) {
            System.out.println("JSON 解析失败：" + e.getMessage());
            return "{}";
        }
    }
}